package com.delong.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.model.entity.*;
import com.delong.lease.model.enums.ItemType;
import com.delong.lease.web.admin.mapper.*;
import com.delong.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delong.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.delong.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.delong.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.delong.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.delong.lease.web.admin.vo.fee.FeeValueVo;
import com.delong.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    /**
     * 保存或更新公寓信息
     * @param apartmentSubmitVo
     */
    @Override
    public void apartmentSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //判断公寓id是否为null，获取boolean
        Long apartmentSubmitVoId = apartmentSubmitVo.getId();
        boolean is = apartmentSubmitVoId != null;
        //将前端传过来的参数保存
        Long provinceId = apartmentSubmitVo.getProvinceId();
        String provinceName = provinceInfoService.getById(provinceId).getName();
        Long cityId = apartmentSubmitVo.getCityId();
        String cityName = cityInfoService.getById(cityId).getName();
        Long districtId = apartmentSubmitVo.getDistrictId();
        String districtName = districtInfoService.getById(districtId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);
        saveOrUpdate(apartmentSubmitVo);
        //boolean不为空更新相关信息 更新(删除 + 插入)
        if (is){
            //删除公寓配套关联信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVoId);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            //删除公寓标签关联信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVoId);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            //删除公寓杂费关联信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVoId);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
            //删除公寓图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVoId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //boolean为空 保存插入相关信息
        //保存公寓配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //判断前端传过来公寓配套的数据不为空
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> apartmentFacilityArrayList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                //建造者模式
                ApartmentFacility facility = ApartmentFacility.builder()
                        .facilityId(facilityInfoId)
                        .apartmentId(apartmentSubmitVoId)
                        .build();
                apartmentFacilityArrayList.add(facility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityArrayList);
        }
        //保存公寓标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> apartmentLabelArrayList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel label = ApartmentLabel.builder()
                        .labelId(labelId)
                        .apartmentId(apartmentSubmitVoId)
                        .build();
                apartmentLabelArrayList.add(label);
            }
            apartmentLabelService.saveBatch(apartmentLabelArrayList);
        }
        //保存公寓杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> feeValueArrayList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = ApartmentFeeValue.builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentSubmitVoId)
                        .build();
                feeValueArrayList.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(feeValueArrayList);
        }
        //保存公寓图片信息（没有中间表，不能用建造者模式，用的话返回json会出问题）
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVoId);
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoArrayList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoArrayList);
        }
    }

    /**
     * 根据条件分页查询公寓列表
     * @param apartmentItemVoPage
     * @param queryVo
     */
    @Override
    public void pageList(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
//        apartmentItemVoPage = apartmentInfoMapper.pageList(apartmentItemVoPage, queryVo);
        apartmentInfoMapper.pageList(apartmentItemVoPage, queryVo);
    }

    /**
     * 根据ID获取公寓详细信息
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo getDetail(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = getById(id);
        if (apartmentInfo == null){
            return null;
        }
        //查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByApartmentId(id);
        //查询标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByApartmentId(id);
        //查询杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueByApartmentId(id);
        //查询图片
        List<GraphVo> graphVoList = graphInfoMapper.selectGraph(id,ItemType.APARTMENT);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartment(Long id) {
        //查询是否公寓对应的房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除公寓信息
        removeById(id);
        //删除公寓配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility :: getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //删除公寓杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        //删除公寓图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}




